package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.pojo.dto.ListGroupAddDTO;
import com.xbongbong.paas.pojo.dto.ListGroupDeleteDTO;
import com.xbongbong.paas.pojo.dto.ListGroupGetDTO;
import com.xbongbong.paas.pojo.dto.ListGroupListDTO;
import com.xbongbong.paas.pojo.dto.ListGroupUpdateDTO;
import com.xbongbong.paas.pojo.vo.GroupListVO;
import com.xbongbong.paas.pojo.vo.ListGroupAddVO;
import com.xbongbong.paas.pojo.vo.ListGroupDeleteVO;
import com.xbongbong.paas.pojo.vo.ListGroupGetVO;
import com.xbongbong.paas.pojo.vo.ListGroupUpdateVO;
import com.xbongbong.paas.service.PaasListGroupService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.thread.TransmittableThreadLocalUtil;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.publicgroup.pojo.dto.PublicGroupSeniorSortDTO;
import com.xbongbong.pro.publicgroup.pojo.vo.PublicGroupSeniorSortVO;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.service.PublicGroupSeniorSortService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author: wufeng
 * @date: 2018/9/7 15:26
 * @desrcption: 分组管理
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/mobile/group")
public class MobilePaasListGroupController {

    @Resource
    private PaasListGroupService paasListGroupService;
    @Resource
    private PublicGroupSeniorSortService publicGroupSeniorSortService;

    /**
     * 列表分组（分组管理列表）
     * @param groupListDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid ListGroupListDTO groupListDTO, BindingResult br){
        XbbResponse<GroupListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(groupListDTO.getSaasMark(), groupListDTO.getBusinessType(), groupListDTO.getMenuId(), groupListDTO.getDistributorMark());
                GroupListVO groupListVO;
                if (Objects.equals(groupListDTO.getSubBusinessType(),XbbRefTypeEnum.CUSTOMER_PUBLIC.getCode()) || Objects.equals(groupListDTO.getSubBusinessType(),XbbRefTypeEnum.CLUE_PUBLIC.getCode())){
                    groupListVO = paasListGroupService.publicList(groupListDTO);
                }else {
                    groupListVO = paasListGroupService.list(groupListDTO);
                }
                response = new XbbResponse<>(groupListVO);
            } catch (XbbException e){
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取公海池列表的分组列表(公海池高级模式下)
     * @return
     */
    @RequestMapping(value = "/publicGroup/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getList(@RequestBody @Valid PublicGroupSeniorSortDTO publicGroupSeniorSortDTO, BindingResult br) {
        XbbResponse<PublicGroupSeniorSortVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(publicGroupSeniorSortDTO.getSaasMark(), publicGroupSeniorSortDTO.getBusinessType(),null , publicGroupSeniorSortDTO.getDistributorMark());
                PublicGroupSeniorSortVO publicGroupSeniorSortVO = publicGroupSeniorSortService.getListByFormIdAndCreatorId(publicGroupSeniorSortDTO);
                response = new XbbResponse<>(publicGroupSeniorSortVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 添加列表分组
     * @param listGroupAddDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String add(@RequestBody @Valid ListGroupAddDTO listGroupAddDTO, BindingResult br){
        XbbResponse<ListGroupAddVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ListGroupAddVO groupListVO = paasListGroupService.add(listGroupAddDTO);
                response = new XbbResponse<>(groupListVO);
            } catch (XbbException e){
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 更新列表分组
     * @param listGroupUpdateDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String update(@RequestBody @Valid ListGroupUpdateDTO listGroupUpdateDTO, BindingResult br){
        XbbResponse<ListGroupUpdateVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ListGroupUpdateVO listGroupUpdateVO = paasListGroupService.update(listGroupUpdateDTO);
                response = new XbbResponse<>(listGroupUpdateVO);
            } catch (XbbException e){
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 删除列表分组
     * @param listGroupDeleteDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delete(@RequestBody @Valid ListGroupDeleteDTO listGroupDeleteDTO, BindingResult br){
        XbbResponse<ListGroupDeleteVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ListGroupDeleteVO listGroupDeleteVO = paasListGroupService.delete(listGroupDeleteDTO);
                response = new XbbResponse<>(listGroupDeleteVO);
            } catch (XbbException e){
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取列表分组详情
     * @param listGroupGetDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String get(@RequestBody @Valid ListGroupGetDTO listGroupGetDTO, BindingResult br){
        XbbResponse<ListGroupGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ListGroupGetVO listGroupGetVO = paasListGroupService.get(listGroupGetDTO);
                response = new XbbResponse<>(listGroupGetVO);
            } catch (XbbException e){
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
